package cn.zhangchg.meituan;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class BService {

    private AService aService;

    /**
     * 批量获取数据
     * @param ids
     * @return
     */
    public List<Integer> get(List<Integer> ids){
        if (ids == null || ids.isEmpty()){
            return new ArrayList<>();
        }
        if (ids.size() > 100){
            throw new RuntimeException("id超出100");
        }
        List<List<Integer>> idLists = new ArrayList<>();
        int length = 10;
        int count = (ids.size() + length - 1) / length;

        //拆分list
        for (int i = 0; i < count; i++) {
            List<Integer> subList = ids.subList(i * length, Math.min(((i + 1) * length), ids.size()));
            idLists.add(subList);
        }
        List<Integer> result = new ArrayList<>(ids.size());
        //批量获取结果并组装
        idLists.parallelStream().map(item -> aService.get(item)).forEach(result::addAll);
        System.out.println(result.size());
        return result;
    }

    /**
     * 获取酒店信息信息
     * @param poiIds
     * @return
     */
    public List<Hotel> batchGetHotelByPoiId(List<Long> poiIds){
        List<Hotel> list;
        Random random = new Random();
        list = poiIds.stream().map(poiId -> new Hotel(poiId, Long.toString(random.nextLong()))).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(list));
        return list;
    }
    /**
     * 获取随机数
     * @return
     */
    public int get(){
        Random random = new Random();
        return random.nextInt(10) + 1;
    }

    /**
     * $field.commont
     */
    public AService getAService() {
        return aService;
    }

    public BService setAService(AService aService) {
        this.aService = aService;
        return this;
    }
}
